package com.ithinks.flow.common.utils.middleware.rabbitMQ.simpleQueue;

import com.ithinks.flow.common.utils.middleware.rabbitMQ.ConnectionUtils;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Description  生产者 ————队列 ——————消费者模式
 * @ClassName SimpleQueue
 * @Author MR.LI
 * @date 2020.11.26 11:52
 */
public class SimpleQueueProducer {
    /**
     * 生产者段操作流程
     * 1、创建连接
     * 2、创建通道
     * 3、声明队列
     * 4、发送消息
     */

    //队列名称
    private static String QUEUE_NAME = "hello1";
    //主机IP
    private static String IP = "localhost";
    //端口
    private static int PORT = 5672;
    //虚拟机IP，默认/
    private static String VHOST = "/";
    //队列名称
    private static String USERNAME = "guest";

    private static String PASSWORD = "guest";

    public static void main(String[] args) throws IOException, TimeoutException {

        //获取连接
        Connection connection = ConnectionUtils.getConnection(IP,PORT, VHOST,USERNAME,PASSWORD);
        Channel channel = null;

        try {
            //1、创建通道
            channel = connection.createChannel();
            //2、声明队列名称
            channel.queueDeclare(QUEUE_NAME,false,false,false,null);
            //3、简单消息发布
            String message = "HELLO ithinks flow !"+System.currentTimeMillis();
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println("Send Message is:'" + message + "'");

        } catch (Exception e) {
            e.printStackTrace();
        } finally{

            if(channel != null)
            {
                channel.close();
            }
            if(connection != null)
            {
                connection.close();
            }
        }
    }
}
